﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Dashboard.Common
{
    /// <summary>
    /// 工具类：用于方便进行HTTP请求
    /// </summary>
    public class HttpTool
    {
        /// <summary>
        /// 发送Get请求并得到一个字符串
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async static Task<string> RequestByGet(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromMilliseconds(15000);
                using (HttpResponseMessage response = await client.GetAsync(url))
                {
                    try
                    {
                        // 若：请求成功且状态码是200
                        if (response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            // 则：返回响应数据的字符串
                            return await response.Content.ReadAsStringAsync();
                        }
                        // 否：
                        else
                        {
                            // 则：构造一个错误响应数据对应并转成Json字符串返回
                            return "2";
                        }
                    }
                    catch (Exception e)
                    {
                        // 若：出现了异常，则：将异常信息封装成错误响应数据对应的Json字符串返回
                        return "3";
                    }
                }
            }
        }

        /// <summary>
        /// 发送Post请求并得到一个Json字符串
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string RequestByPost(string url, object data)
        {
            try
            {
                HttpContent param = new StringContent(JsonConvert.SerializeObject(data));
                param.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                param.Headers.ContentType.CharSet = "UTF-8";
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromMilliseconds(15000);
                    using (HttpResponseMessage response = client.PostAsync(url, param).Result)
                    {
                        // 若：请求成功且状态码是200
                        if (response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            // 则：返回响应数据的字符串
                            return response.Content.ReadAsStringAsync().Result;
                        }
                        // 否：
                        else
                        {
                            // 则：构造一个错误响应数据对应并转成Json字符串返回
                            return "2";
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // 若：出现了异常，则：将异常信息封装成错误响应数据对应的Json字符串返回
                return "3";
            }
        }
    }
}
